package com.cloudree.gllib.render;

import android.opengl.GLES20;
import android.opengl.Matrix;
import android.provider.Settings;

import com.cloudree.gllib.shape.HockeyTable;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * Created by Administrator on 2017/8/29.
 */
public class HockeyRender extends BaseRender {

    private int w;
    private int h;

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        mShape = new HockeyTable();
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
        float aspectRatio = width > height ? (float) width / height : (float) height / width;

//        if (width > height) {
//            Matrix.orthoM(mProjectionMatrix, 0, -aspectRatio, aspectRatio, -1.0f, 1.0f, -1.0f, 1.0f);
//        } else {
//            Matrix.orthoM(mProjectionMatrix, 0, -1.0f, 1.0f, -aspectRatio, aspectRatio, -1.0f, 1.0f);
//        }

        w = width;
        h = height;

    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

        //perspective projection
        Matrix.perspectiveM(mProjectionMatrix, 0, 45, (float) w / h, 1.0f, 10.0f);

        Matrix.setIdentityM(mModelMatrix, 0);
        Matrix.translateM(mModelMatrix, 0, 0, 0, -3.0f);
        Matrix.rotateM(mModelMatrix, 0, -60.0f, 1.0f, 0, 0);
        float[] temp = new float[16];
        Matrix.multiplyMM(temp, 0, mProjectionMatrix, 0, mModelMatrix, 0);
        System.arraycopy(temp, 0, mProjectionMatrix, 0, temp.length);

        mShape.draw(mMVPMatrix, mModelMatrix, mViewMatrix, mProjectionMatrix);
    }
}
